﻿#include "word_dict.h"

WordDic::~WordDic(){}

bool WordDic::getCandidateList(const std::string &code, StringListType &out_list){
    auto it = _dic.find(code);

    if(it == _dic.end()){
        return false;
    }
    out_list = it->second;
    return true;
}

void WordDic::addWord(const std::string &code, const std::string &word) {
    auto& candidates = _dic[code];
    // 检查候选词是否已存在
    if (std::find(candidates.begin(), candidates.end(), word) == candidates.end()) {
        candidates.push_back(word);
    }
}

void WordDic::setWords(std::vector<std::pair<std::string, std::string>> &words){
    _dic.clear();
    if(words.empty()){
        return;
    }
    _dic.rehash(words.size());
    bool is_exit = false;
    for(auto& word : words){
        auto& candidates = _dic[word.first];
        is_exit = false;
        for(auto &candidate : candidates){
            if(candidate == word.second){
                is_exit = true;
                break;
            }
        }
        if(!is_exit)
            candidates.push_back(word.second);
    }
}

void WordDic::clear() {
    _dic.clear();
}

WordDic::WordDic()
{
}
